Page({
  data: {
    id: '...', //我的排名
    amount: '...',
    aTags: '',
    cutColor: '#cbcbcb88',
    aibiImage1: '../../images/lovecoin_before.png',
    aibiImage: '../../images/lovecoin_after.png',
    // url: '../../images/lovecoin_before.png',
    isShow1: false,
    m: '', //保存我的userId
    person: [],
    pipei: 0,
    sessionid: null, //登录判断
    bgImage: '../../images/bg.png',
    rankBg: '../../images/rank_bg.png',
  },

  paiHang: function () {
    var that = this
    wx.request({
      //排行数据
      url: getApp().globalData.ip + '/ranking/donate',
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=utf-8',
      },
      dataType: 'json',
      success: function (res1) {
        // console.log('助力排行：', res1.data)
        that.setData({
          person: res1.data,
        })
        let person = that.data.person
        for (let i in person) {
          let dtPerson = wx.getStorageSync('dtPerson')
          let DataUserId = person[i].userId
          for (let j = 0; j < dtPerson.length; j++) {
            let StorUserId = dtPerson[j].userId
            if (StorUserId == DataUserId) {
              person[i].isShow = dtPerson[j].isShow
            }
          }
          if (person[i].isShow === false) {
            person[i].isShow = false
          }
        }
        that.setData({
          person,
        })
      },
    })
  },

  change(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    let zanInfo = getApp().globalData.ip + '/ranking/atags'
    let person = this.data.person
    var uid = this.data.person[index].userId
    let aTags = that.data.aTags
    // var isshow = person[index].isShow;
    if (!person[index].isShow) {
      person[index].atags++
      person[index].isShow = true
      this.setData({
        person,
      })
      if (that.data.m === e.currentTarget.dataset.userid) {
        that.setData({
          aTags: aTags + 1,
        })
      }
      wx.setStorageSync('dtPerson', person)
      wx.request({
        url: `${zanInfo}/${uid}`,
        data: {},
        header: {
          cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
        },
        method: 'PUT',
        type: 'json',
        success(res) {},
      })
    }
  },

  myPaiHang() {
    //返回我的排名和次数
    var that = this
    var r = getApp().globalData.ip + '/ranking/donate'
    var uid = that.data.m
    console.log('uid：', that.data.m)
    wx.request({
      url: `${r}/${uid}`,
      header: {
        cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
      },
      method: 'GET',
      type: 'json',
      success(r_s) {
        console.log('我的排名:', r_s)
        that.setData({
          id: r_s.data.id,
          amount: r_s.data.amount,
        })
      },
    })
  },

  onLoad: function () {
    var that = this
    that.setData({
      sessionid: getApp().globalData.sessionid,
    })
    that.paiHang()
    wx.request({
      //我的账户
      url: getApp().globalData.ip + '/account/detail',
      header: {
        cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
      },
      method: 'GET',
      success: function (res2) {
        that.setData({
          aTags: res2.data.data.aTags,
          m: res2.data.data.userId,
        })
        that.myPaiHang() // 异步
      },
    })

    //请求完成
    let newTime = Date.parse(new Date())
    let oldTime = wx.getStorageSync('oldTime')
    if (oldTime && oldTime > newTime) {
      let dtPerson = wx.getStorageSync('dtPerson')
      that.setData({
        person: dtPerson,
      })
    } else {
      let oldTime = newTime + 86400000
      wx.setStorageSync('oldTime', oldTime)
      wx.removeStorageSync('dtPerson')
      that.paiHang()
    }
  },

  onPullDownRefresh: function () {
    this.onLoad()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },
})
